<template>
  <div class="container">
    <div class="app-container">
      <el-tree :default-expand-all="true" :expand-on-click-node="false" :data="depts" :props="defaultProps">
        <template #default="{data}">
          <el-row style="width:100%;height:40px;" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree_manager">{{ data.managerName }}</span>
              <!-- $event理解为el-dropdown内部封装了点击的原生js事件对象，通过$event返回item的command -->
              <el-dropdown @command="operateDept($event,data.id)">
                <span class="el-dropdown-link">
                  管理<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 放置弹层 -->
    <!-- ref获取当前自定义组件实例对象,也就是addDept的this -->
    <addDept ref="addDept" :show-title="showTitle" :current-id="currentId" :show-dialog.sync="showDialog" @updateDepartment="getDepartmentList" />
  </div>
</template>

<script>
import { _deleteDepartment, _getDepartmentList } from '@/api/department'
import { tranListToTreeData } from '@/utils/index'
import addDept from './component/addDept.vue'
export default {
  name: 'Department',
  components: { addDept },
  data() {
    return {
      showTitle: '新增部门',
      currentId: null,
      showDialog: false,
      depts: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await _getDepartmentList()
      console.log(res) // 数组包数组
      this.depts = tranListToTreeData(res, 0)
    },
    operateDept(type, id) {
      // console.log(type)
      if (type === 'add') {
        this.showDialog = true
        this.currentId = id
        this.showTitle = '新增部门'
      } else if (type === 'edit') {
        this.showDialog = true
        this.currentId = id
        this.showTitle = '编辑部门'
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        this.$confirm('您确定要删除该部门吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(async() => {
          await _deleteDepartment()
          this.$message.success('删除该部门成功')
          this.getDepartmentList()
        })
      }
    }
  }
}
</script>

<style scoped>
  .app-container{
    padding: 30px 140px;
    font-size: 14px;
  }
  .tree_manager{
    width: 50px;
    display: inline-block;
    margin:30px;
  }
</style>
